package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AddressDao;
import ex.com.jm.food.entity.Citys;
import ex.com.jm.food.entity.Countys;
import ex.com.jm.food.entity.Province;
import ex.com.jm.food.entity.Streets;

public class AddressDaoImpl extends HibernateDaoSupport implements AddressDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Province> getProvinces() {
		// TODO Auto-generated method stub
		Object provinces=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createSQLQuery("select * from jm_province order by pro_id desc").addEntity("province", Province.class);
				return query.list();
			}
		});
		return (List<Province>) provinces;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Citys> getCitys(final int proId) {
		// TODO Auto-generated method stub
		Object citys=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createSQLQuery("select * from jm_citys where pro_id="+proId).addEntity("city",Citys.class);
				return query.list();
			}
		});
		return (List<Citys>) citys;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Countys> getCountys(final int cityId) {
		// TODO Auto-generated method stub
		Object countys=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createSQLQuery("select * from jm_countys where city_id="+cityId).addEntity("county", Countys.class);
				return query.list();
			}
		});
		return (List<Countys>) countys;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Streets> getStreets(final int countyId) {
		// TODO Auto-generated method stub
		Object streets=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createSQLQuery("select * from jm_streets where county_id="+countyId).addEntity("streets",Streets.class);
				return query.list();
			}
		});
		return (List<Streets>) streets;
	}
}
